/**
 * Copyright &copy; 2020 <a href="https://github.com/somowhere/exam-system">exam-system</a> All rights reserved.
 */
package com.shanhesoft.java.modules.exam.web;


import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.ApiOperation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanhesoft.java.common.core.constant.CommonConstants;
import com.shanhesoft.java.common.core.util.Result;
import com.shanhesoft.java.common.core.vo.PageModel;
import com.shanhesoft.java.common.data.util.QueryWrapperUtil;
import com.shanhesoft.java.common.log.annotation.LogOperate;
import com.shanhesoft.java.common.log.enums.BusinessType;
import com.shanhesoft.java.common.web.resource.BaseResource;
import com.shanhesoft.java.modules.exam.domain.dto.OjSubmissionsDto;
import com.shanhesoft.java.modules.exam.domain.dto.OjSubmissionsQueryCriteria;
import com.shanhesoft.java.modules.exam.service.OjSubmissionsService;

import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;

import javax.validation.Valid;
import java.util.Set;

/**
 * 提交记录Controller 提交记录
 * @author admin
 * @version 2020-09-26 20:35:59
 */
@RestController
@RequestMapping(value = "${application.admin-path}/exam/ojSubmissions")
@Slf4j
@AllArgsConstructor
public class OjSubmissionsResource extends BaseResource {

	private final OjSubmissionsService service;

	/**
	 * @param id
	 * @return
	 */
	@GetMapping(CommonConstants.URL_ID_REGEX)
	@PreAuthorize("@pms.hasPermission('exam_ojSubmissions_view')")
	public Result get(@PathVariable String id) {
		log.debug("REST request to get Entity : {}", id);
		return  Result.buildOkData(service.findVoById(id));
	}
	/**
	 * GET / : get all ojSubmissions.
	 *
	 * @param pm the pagination information
	 * @return the Result with status 200 (OK) and with body all ojSubmissions
	 */

	@PreAuthorize("@pms.hasPermission('exam_ojSubmissions_view')")
	@GetMapping
	@ApiOperation(value = "提交记录查看")
	public Result getPage(PageModel pm, OjSubmissionsQueryCriteria ojSubmissionsQueryCriteria) {
		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, ojSubmissionsQueryCriteria);
		return Result.buildOkData(service.getListPage(pm, wrapper));
	}

	@GetMapping("/getUserList")
	@ApiOperation(value = "查看正确答题人列表")
	public Result getUserList(PageModel pm, OjSubmissionsQueryCriteria ojSubmissionsQueryCriteria) {
		return Result.buildOkData(service.getUserList(pm, ojSubmissionsQueryCriteria));
	}

	/**
	 * POST / : Save a ojSubmissionsDto.
	 *
	 * @param ojSubmissionsDto the HTTP ojSubmissions
	 */
	@PreAuthorize("@pms.hasPermission('exam_ojSubmissions_edit')")
	@ApiOperation(value = "提交记录编辑")
	@PostMapping
	public Result save(@Valid @RequestBody OjSubmissionsDto ojSubmissionsDto) {
		log.debug("REST request to save OjSubmissionsDto : {}", ojSubmissionsDto);
		service.saveOrUpdate(ojSubmissionsDto);
		return Result.buildOk("保存提交记录成功");

	}

	@ApiOperation(value = "提交并返回保存对象")
	@PostMapping("/saveReturn")
	public Result saveReturn(@Valid @RequestBody OjSubmissionsDto ojSubmissionsDto) {
		log.debug("REST request to save OjSubmissionsDto : {}", ojSubmissionsDto);
		return Result.buildOkData(service.saveOrUpdateReturn(ojSubmissionsDto), "保存提交记录成功");

	}

	/**
	 * DELETE //:ids : delete the "ids" OjSubmissions.
	 *
	 * @param ids the id of the ojSubmissions to delete
	 * @return the Result with status 200 (OK)
	 */
	@PreAuthorize("@pms.hasPermission('exam_ojSubmissions_del')")
	@ApiOperation(value = "提交记录删除")
	@DeleteMapping
	public Result delete(@RequestBody Set<String> ids) {
		log.debug("REST request to delete OjSubmissions: {}", ids);
		service.removeByIds(ids);
		return Result.buildOk("删除提交记录成功");
	}
}
